package com.zw.pdm.module.report.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.rep.Change;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.entity.rep.VerifyProcess;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.report.model.SheetModel;
import com.zw.pdm.module.report.service.ReportCheckService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.*;

/**
 * @Description: 计划审核接口
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/report/check")
@SuppressWarnings("all")
public class ReportCheckController extends BaseController {

    @Autowired
    private ReportCheckService reportCheckService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;

    @GetMapping
    @RequiresPermissions({"report:check:view"})
    public String page(Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        // 查部级审核人，用于修改审核人功能
        model.addAttribute("users", reportCheckService.findVerifier());
        model.addAttribute("secondUsers", reportCheckService.findSecondUser());
        return "report/check/page";
    }

    /** 计划单审核列表 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"report:check:view"})
    public JsonResult sheetList(SheetModel sheet) {
        startPage();
        List<SheetModel> list = reportCheckService.findWithReportCheck(sheet);
        return jsonResult(list);
    }

    /** 计划单查看页面 */
    @GetMapping("/info/{id}")
    @RequiresPermissions({"report:check:view"})
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("sheet", sheet);
        model.addAttribute("process", sheetService.findVpBySheetId(sheetId));
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", sheetService.findPlanBySheetId(sheetId));
            return "report/list/print";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", sheetService.findChangeBySheetId(sheetId));
            return "report/list/changePrint";
        }
    }

    /** 计划单审核页面 */
    @GetMapping("/{id}")
    @RequiresPermissions({"report:check:view"})
    public String verify(@PathVariable("id") Integer sheetId, Model model) {
        Sheet sheet = sheetService.getSheetById(sheetId);
        model.addAttribute("sheet", sheet);
        model.addAttribute("process", sheetService.findVpBySheetId(sheetId));
        if (sheet.getSheetType() == 1) {
            model.addAttribute("plans", sheetService.findPlanBySheetId(sheetId));
            return "report/check/verify";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", sheetService.findChangeBySheetId(sheetId));
            return "report/check/changeVerify";
        }
    }

    /** 审核计划 */
    @ResponseBody
    @PostMapping("/success")
    @RequiresPermissions({"report:check:edit"})
    public JsonResult success(Integer id) {
        return jsonResult(reportCheckService.verify(id));
    }

    /** 驳回计划 */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions({"report:check:edit"})
    public JsonResult reject(Sheet sheet) {
        return jsonResult(reportCheckService.reject(sheet));
    }

    /** 修改审核人 */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"report:check:editVerify"})
    public JsonResult editVerify(VerifyProcess verifyProcess) {
        return isNotNull(reportCheckService.editVerify(verifyProcess));
    }

    /**
     * 修改上级审核人（制作件清单）
     */
    @ResponseBody
    @PostMapping("/makeEdit")
    public JsonResult editMakeVerify(VerifyProcess verifyProcess) {
        return isNotNull(reportCheckService.editMakeVerify(verifyProcess));
    }

    /** 修改第二提报人 */
    @ResponseBody
    @PostMapping("/editReport")
    @RequiresPermissions({"report:check:second"})
    public JsonResult editSecondUser(Sheet sheet) {
        return isNotNull(reportCheckService.editSecondUser(sheet));
    }

    /** 取消变更页面 */
    @GetMapping("/cancel/{id}")
    @RequiresPermissions({"report:check:edit"})
    public String cancelPage(@PathVariable("id") Integer sheetId, Model model) {
        model.addAttribute("sheet", sheetService.getSheetById(sheetId));
        return "report/check/cancelPage";
    }

    /** 变更计划列表 */
    @ResponseBody
    @PostMapping("/change/list")
    @RequiresPermissions({"report:check:edit"})
    public JsonResult planList(Change change) {
        startPage();
        List<Change> list = reportCheckService.findChangeDetail(change);
        return jsonResult(list);
    }

    /** 取消变更 */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"report:check:edit"})
    public JsonResult cancelChange(Integer id) {
        return jsonResult(reportCheckService.cancelChange(id));
    }

    /**
     * 对物资系统接口， 退回单条计划
     */
    @ResponseBody
    @PostMapping("/return")
    @CrossOrigin
    public boolean returnSheet(Integer resultId) {
        if (resultId == null) {
            return false;
        }
        return reportCheckService.returnSheet(resultId);
    }

}
